google cloud api | list google bucket url map | wait for cloud operation | Search

This code is a test suite that uses various functions to list different resources (url maps, target proxies, global forwards, backend buckets) from a Google Cloud project, and asserts that each list is non-empty. The test suite uses functions such as listUrlMaps, listTargetProxies, listGlobalForwards, and listBackendBuckets to retrieve the lists of resources and logs the results to the console.

Run example

npm run import -- "test list url map"

test list url map

var assert = require('assert');
var importer = require('../Core');
var {
    listUrlMaps,
    listTargetProxies,
    listGlobalForwards,
    listBackendBuckets
} = importer.import("list google bucket url map");
var project = 'spahaha-ea443';
var map = 'web-map';
var proxy = 'web-map-target-proxy-5';

describe('listing subdomains from load balancer', () => {
    
    it('should list buckets maps', () => {
        return listUrlMaps(project)
            .then(items => {
                console.log(items[Object.keys(items)[0]].hostRules);
                console.log(items[Object.keys(items)[0]].pathMatchers);
                console.log(items[Object.keys(items)[0]].pathMatchers[0].pathRules);
                assert(Object.keys(items).length > 0, 'should have a url mapper');
                
                return listTargetProxies(
                    project,
                    map);
            })
            .then(proxies => {
                console.log(proxies);
                assert(proxies.length > 0, 'should have a proxy');
            
                return listGlobalForwards(
                    project,
                    proxy);
            })
            .then(rules => {
                console.log(rules);
                assert(Object.keys(rules).length > 0, 'should have a rule');
            
                return listBackendBuckets(project);
            })
            .then(buckets => {
                console.log(buckets);
                assert(Object.keys(buckets).length > 0, 'should have a bucket');
            });
    })
    
    
})

What the code could have been:

const assert = require('assert');
const { listUrlMaps, listTargetProxies, listGlobalForwards, listBackendBuckets } = require('../Core');

describe('listing subdomains from load balancer', () => {
    const project ='spahaha-ea443';
    const map = 'web-map';
    const proxy = 'web-map-target-proxy-5';

    it('should list buckets maps', async () => {
        // Get list of URL maps
        const urlMaps = await listUrlMaps(project);
        if (!urlMaps || Object.keys(urlMaps).length === 0) {
            throw new Error('No URL maps found');
        }
        const urlMap = urlMaps[Object.keys(urlMaps)[0]];
        console.log('URL Map Host Rules:', urlMap.hostRules);
        console.log('URL Map Path Matchers:', urlMap.pathMatchers);
        console.log('Path Matcher Path Rules:', urlMap.pathMatchers[0].pathRules);

        // Get list of target proxies
        const proxies = await listTargetProxies(project, map);
        assert(proxies.length > 0,'should have a proxy');
        console.log('Target Proxies:', proxies);

        // Get list of global forwards
        const rules = await listGlobalForwards(project, proxy);
        assert(Object.keys(rules).length > 0,'should have a rule');
        console.log('Global Forwards:', rules);

        // Get list of backend buckets
        const buckets = await listBackendBuckets(project);
        assert(Object.keys(buckets).length > 0,'should have a bucket');
        console.log('Backend Buckets:', buckets);
    });
});

Code Breakdown

Dependencies and Imports

Variables

Test Suite

Functions Called

Assertions